package org.unt.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.unt.genlogic.Value;
import org.unt.logic.StringLogic;

public class DbLogic {

	public static void main(String[] args) throws Exception {
		getStructure();
	}

	public static void getStructure() throws Exception {

		Properties properties = StringLogic.load("config.properties");
		Db db = new Db(properties, "db2pnr");
		// db.getColumnsFromTable("", "");

		ResultSet rs = db.select("select count(*) from PNR000.PNRQTPVE");
		rs.next();
		int i = rs.getInt(1);
		System.out.println(i);
	}

	/**
	 * @param type
	 * @param scale
	 * @return DECIMAL INTEGER VARCHAR TIMESTAMP DATE
	 */
	public static String doStandardType(String type, int scale) {

		if ("NUMBER".equals(type) && scale > 0) {

			return "DECIMAL";

		} else if ("INTEGER".equals(type) || "SMALLINT".equals(type) || ("NUMBER".equals(type) && scale == 0)) {

			return "INTEGER";

		} else if ("CLOB".equals(type) || "VARCHAR".equals(type) || "VARCHAR2".equals(type) || "CHAR".equals(type)) {

			return "VARCHAR";

		} else if ("DECIMAL".equals(type)) {

			return "DECIMAL";

		} else if ("TIMESTMP".equals(type) || "TIMESTAMP".equals(type) || "TIMESTAMP(9)".equals(type)) {

			return "TIMESTAMP";

		} else if ("DATE".equals(type)) {

			return "DATE";

		} else if ("FLOAT".equals(type)) {

			return "DECIMAL";

		} else {
			throw new RuntimeException("Unknown type " + type);
		}
	}

	/**
	 * From an array of "Table1(int)" ... "Table2(str)" get "(45,'hola')"
	 */
	public static String buildValues(ResultSet rs, String[] cols) throws SQLException {

		StringBuffer values = new StringBuffer("(");
		for (int i = 0; i < cols.length; i++) {

			String column = cols[i].substring(0, cols[i].indexOf("("));
			String type = cols[i].substring(cols[i].indexOf("(") + 1, cols[i].indexOf(")"));

			if ("str".equals(type)) {
				values.append("'" + rs.getString(column) + "',");
			} else if ("num".equals(type)) {
				values.append(rs.getString(column));
			} else if ("dat".equals(type)) {
				values.append("'" + rs.getString(column) + "',");
			} else if ("tim".equals(type)) {
				values.append("'" + rs.getString(column) + "',");
			}
		}
		values.deleteCharAt(values.length() - 1);
		values.append(")");

		return values.toString();
	}

	public static List buildValuesList(ResultSet rs, String[] cols) throws SQLException {

		List values = new ArrayList();

		for (int i = 0; i < cols.length; i++) {

			String column = cols[i].substring(0, cols[i].indexOf("("));
			String type = cols[i].substring(cols[i].indexOf("(") + 1, cols[i].indexOf(")"));

			if ("str".equals(type)) {

				values.add(new Value(rs.getString(column), String.class));

			} else if ("int".equals(type) || "sma".equals(type)) {

				if (rs.getObject(column) == null) {
					values.add(new Value(null, Integer.class));
				} else {
					values.add(new Value(new Double(rs.getInt(column)), Integer.class));
				}
			} else if ("dec".equals(type)) {

				values.add(new Value(new Double(rs.getDouble(column)), Double.class));

			} else if ("dat".equals(type)) {

				values.add(new Value(rs.getDate(column), java.sql.Date.class));

			} else if ("tim".equals(type)) {

				values.add(new Value(rs.getTimestamp(column), Timestamp.class));
			}
		}

		return values;
	}

	public static String valuesToString(String[] cols, List values) {

		StringBuffer buf = new StringBuffer();

		int i = 0;
		for (Iterator iterator = values.iterator(); iterator.hasNext(); i++) {

			Value value = (Value) iterator.next();
			buf.append(cols[i] + ":" + value + ", ");
		}

		buf.deleteCharAt(buf.length() - 1);
		buf.deleteCharAt(buf.length() - 1);

		return buf.toString();
	}

	public static boolean compareColumns(Column column1, Column column2, boolean doLengthComparison) {

		boolean compareName = compareColumnNames(column1.getName(), column2.getName());
		boolean compareType = compareColumnTypes(column1.getType(), column2.getType());
		boolean compareLength = compareColumnLength(column1.getLength(), column2.getLength(), column1.getType());
		boolean compareScale = compareColumnScale(column1.getScale(), column2.getScale());
		boolean compareNullable = (column1.isNullable() && column2.isNullable()) || (!column1.isNullable() && !column2.isNullable());

		// compareScale = true;

		if (!doLengthComparison) {
			compareLength = true;
		}

		return compareName && compareType && compareLength && compareScale && compareNullable;
	}

	private static boolean compareColumnScale(int scale1, int scale2) {

		return scale1 == scale2;
	}

	private static boolean compareColumnLength(int length1, int length2, String type) {

		if (type.indexOf("CHAR") > -1) {
			return length1 == length2;
		}

		return true;
	}

	private static boolean compareColumnTypes(String type1, String type2) {

		if (type1.equals(type2)) {

			return true;

			// } else if ("DECIMAL".equals(type1) && "INTEGER".equals(type2) || "DECIMAL".equals(type1) && "INTEGER".equals(type2)) {
			// return true;
		}

		return false;
	}

	private static boolean compareColumnNames(String col1, String col2) {

		if (col1.equals(col2)) {

			return true;

		} else if ("COMMENTAIRE".equals(col1) && "COMMENT".equals(col2) || "COMMENT".equals(col1) && "COMMENTAIRE".equals(col2)) {

			return true;

		} else if ("ORDEN".equals(col1) && "ORDER".equals(col2) || "ORDER".equals(col1) && "ORDEN".equals(col2)) {

			return true;

		} else if ("NUMBER".equals(col1) && "NUMERO".equals(col2) || "NUMERO".equals(col1) && "NUMBER".equals(col2)) {

			return true;
		}

		return false;
	}
}
